﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;

namespace blackhouse.BlackApart.Business.PushMails {
    public class DaPushMail : IPushMail {

        #region Fields

        private ImpPushMail instance;
        private IPushMailDataAccess dataAccess;
        private int id = 0;

        #endregion

        #region Properties

        public string[] To {
            get {
                return this.instance.To;
            }
            set {
                this.instance.To = value;
            }
        }

        public string Title {
            get { return this.instance.Title; }
            set { this.instance.Title = value; }
        }

        public string Content {
            get { return this.instance.Content; }
            set { this.instance.Content = value; }
        }

        public bool IsHtml {
            get { return this.instance.IsHtml; }
            set { this.instance.IsHtml = value; }
        }

        #endregion

        #region Constructors

        public DaPushMail(IPushMailDataAccess dataAccess, int id) {
            this.dataAccess = dataAccess;
            if (id == 0)
                this.instance = new ImpPushMail();
            else {
                this.instance = this.Convert(this.dataAccess.GetPushMail(id));
                if (this.instance == null)
                    this.instance = new ImpPushMail();
            }
        }

        #endregion

        #region Methods

        private ImpPushMail Convert(PushMailData data) {
            if (data.Id > 0) {
                ImpPushMail mail = new ImpPushMail();
                mail.Content = data.Content;
                mail.IsHtml = data.IsHtml;
                mail.Title = data.Title;
                mail.To = data.ToMails.Split(';');
                this.id = data.Id;
                return mail;
            } else return null;
        }

        public void Save() {
            this.dataAccess.SavePushMail(this.Content, this.IsHtml, this.Title, string.Join(";", this.To));
        }

        public void MarkAsSended() {
            this.dataAccess.PushMailSended(this.id);
        }

        #endregion

    }
}
